﻿using System;
using System.Windows.Input;

namespace DemoApp.ViewModel
{
    using Songhay.Wpf.Mvvm;
    using Songhay.Wpf.Mvvm.ViewModel;

    /// <summary>
    /// This ViewModelBase subclass requests to be removed 
    /// from the UI when its CloseCommand executes.
    /// This class is abstract.
    /// </summary>
    /// <remarks>
    /// Based on the MVVM pattern from Josh Smith, http://joshsmithonwpf.wordpress.com/
    /// </remarks>
    public abstract class CloseableViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CloseableViewModel"/> class.
        /// </summary>
        protected CloseableViewModel() { }

        /// <summary>
        /// Returns the command that, when invoked, attempts
        /// to remove this workspace from the user interface.
        /// </summary>
        public ICommand CloseCommand
        {
            get
            {
                if(this._closeCommand == null)
                    this._closeCommand = new RelayCommand(param => this.OnRequestClose());
                return this._closeCommand;
            }
        }

        /// <summary>
        /// Raised when this workspace should be removed from the UI.
        /// </summary>
        public event EventHandler RequestClose;

        void OnRequestClose()
        {
            EventHandler handler = this.RequestClose;
            if(handler != null)
                handler(this, EventArgs.Empty);
        }

        RelayCommand _closeCommand;
    }
}